/*
 *    Copyright 2008-2009 The PoCo Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.asemantics.poco.serializer.json;

import java.io.IOException;
import java.io.Writer;

import com.asemantics.poco.serializer.AbstractPoCoSerializer;
import com.asemantics.poco.serializer.PoCoSerializerException;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: JsonPoCoSerializer.java 272 2009-12-21 12:01:38Z simone.tripodi@gmail.com $
 */
public final class JsonPoCoSerializer extends AbstractPoCoSerializer {

    private JsonWriter jsonWriter;

    public JsonPoCoSerializer(Writer writer) {
        super(writer);
    }

    public JsonPoCoSerializer(Writer writer, boolean prettyPrint) {
        super(writer, prettyPrint);
    }

    @Override
    protected void init(Writer writer, boolean prettyPrint) {
        this.jsonWriter = new JsonWriter(writer, prettyPrint);
    }

    @Override
    protected void startDocument() throws PoCoSerializerException {
        try {
            this.jsonWriter.startDocument();
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void startElement(String name) throws PoCoSerializerException {
        try {
            this.jsonWriter.printKey(name);
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void endElement() throws PoCoSerializerException {
        // useless in JSON
    }

    @Override
    protected void startObject() throws PoCoSerializerException {
        try {
            this.jsonWriter.beginObject();
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void startList() throws PoCoSerializerException {
        try {
            this.jsonWriter.beginArray();
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void value(String text, boolean literal)
            throws PoCoSerializerException {
        try {
            this.jsonWriter.printString(text, literal);
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void endObject() throws PoCoSerializerException {
        try {
            this.jsonWriter.endObject();
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void endList() throws PoCoSerializerException {
        try {
            this.jsonWriter.endArray();
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

    @Override
    protected void endDocument() throws PoCoSerializerException {
        try {
            this.jsonWriter.endDocument();
        } catch (IOException e) {
            throw new PoCoSerializerException(e);
        }
    }

}
